package studentManage;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentModel {

	static Scanner sc = new Scanner(System.in);

	private int id;// 学号
	private String name;// 姓名
	private String stu_class;// 班级
	private String sex;// 性别
	private int age;// 年龄
	private int marjorId;// 专业
	private String password;// 密码

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		if (age > 0 && age <= 200) {
			this.age = age;
		} else {
			System.err.println("您输入的年龄不合法！");
		}
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getStu_class() {
		return stu_class;
	}

	public void setStu_class(String stu_class) {
		this.stu_class = stu_class;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		if (sex.equals("男")) {
			this.sex = sex;
		} else if (sex.equals("女")) {
			this.sex = sex;
		} else {
			System.err.println("您输入的性别不合法！");
		}
	}

	public int getMarjorId() {
		return marjorId;
	}

	public void setMarjorId(int marjorId) {
		this.marjorId = marjorId;
	}

	// 查询全部学生信息
	public void print(ArrayList<StudentModel> students, ArrayList<MajorModel> majorModels) {
		System.out.println("***显示全部学生信息***");
		if (students.size() == 0) {
			System.out.println("没有学生信息！");
			return;
		}
		for (int i = 0; i < students.size(); i++) {
			message(students, majorModels, i);
		}
	}

	// 查询学生信息（id）
	public int searchStudent(ArrayList<StudentModel> students, int id) {
		for (int i = 0; i < students.size(); i++) {
			if (students.get(i).getId() == id) {
				return i;
			}
		}
		return -1;
	}

	// 根据学生姓名查询学生信息
	public int searchName(ArrayList<StudentModel> students, String name) {
		for (int i = 0; i < students.size(); i++) {
			if (students.get(i).getName().equals(name)) {
				return i;
			}
		}
		return -1;
	}

	public void message(ArrayList<StudentModel> students, ArrayList<MajorModel> majorModels, int i) {
		if (i == -1) {
			System.out.println("未查询到这个学生！");
			return;
		}
		MajorModel majorModel = new MajorModel();
		System.out.println("*****************");
		System.out.println("学号：" + students.get(i).getId());
		System.out.println("姓名：" + students.get(i).getName());
		System.out.println("性别：" + students.get(i).getSex());
		System.out.println("年龄：" + students.get(i).getAge());
		System.out.println("班级：" + students.get(i).getStu_class());
		// 专业下标
		int k = majorModel.searchById(majorModels, students.get(i).getMarjorId());
		if (k == -1) {
			System.out.println("专业：该专业已被取消或暂未选择专业！");
		} else {
			System.out.println("专业：" + majorModels.get(k).getMajor());
		}
		System.out.println("*****************");

	}

	// 添加学生信息
	public void addStudent(ArrayList<StudentModel> students, ArrayList<MajorModel> majorModels) {
		System.out.println("****添加学生信息****");
		StudentModel stu = new StudentModel();
		MajorModel maj = new MajorModel();

		System.out.println("请输入学生学号");
		String id = sc.nextLine();
		stu.setId(Integer.parseInt(id));

		System.out.println("请输入密码");
		String psw = sc.nextLine();
		stu.setPassword(psw);

		System.out.println("请输入姓名");
		String name = sc.nextLine();
		stu.setName(name);

		System.out.println("请输入性别");
		String sex = sc.nextLine();
		stu.setSex(sex);

		System.out.println("请输入年龄");
		String age = sc.nextLine();
		stu.setAge(Integer.parseInt(age));

		System.out.println("请输入班级");
		String stu_class = sc.nextLine();
		stu.setStu_class(stu_class);

		System.out.println("请输入专业id");
		// 专业信息
		int i = 0;
		for (i = 0; i < majorModels.size(); i++) {
			System.out.println(i + 1 + "--------------------");
			System.out.println("专业id" + majorModels.get(i).getmId());
			System.out.println("专业名称" + majorModels.get(i).getMajor());
		}
		String marjorId = sc.nextLine();
		int mid = Integer.parseInt(marjorId);
		stu.setMarjorId(mid);// 存入专业id
		int k = maj.searchById(majorModels, mid);
		if (k == -1) {
			System.out.println("专业id不存在！");
		} else {
			majorModels.get(k).setStuId(id + " ");
		}

		students.add(stu);

		System.out.println("添加成功！");

		message(students, majorModels, searchStudent(students, Integer.parseInt(id)));
	}

	// 修改学生信息
	public void updataStudent(ArrayList<StudentModel> students, ArrayList<MajorModel> majorModels, int i) {
		// 学生的id默认不能修改

		while (true) {
			System.out.println("*****学生信息******");
			message(students, majorModels, i);
			System.out.println("*****************");
			System.out.println("请选择您要修改的内容：");
			System.out.println("1-姓名");
			System.out.println("2-密码");
			System.out.println("3-性别");
			System.out.println("4-年龄");
			System.out.println("5-班级");
			System.out.println("6-专业");
			System.out.println("7-退出");
			String k = sc.nextLine();
			int key = Integer.parseInt(k);
			switch (key) {

			case 1:
				// 修改姓名
				System.out.print("姓名：" + students.get(i).getName() + "请输入您要修改的姓名：");
				String name = sc.nextLine();
				students.get(i).setName(name);
				System.out.println();
				break;

			case 2:
				// 修改密码
				System.out.print("密码：" + students.get(i).getPassword() + "请输入您要修改的密码：");
				String psw = sc.nextLine();
				students.get(i).setPassword(psw);
				System.out.println();
				break;

			case 3:
				// 修改性别
				System.out.print("性别：" + students.get(i).getSex() + "请输入您要修改的性别：");
				String sex = sc.nextLine();
				students.get(i).setSex(sex);
				System.out.println();
				break;

			case 4:
				// 修改年龄
				System.out.print("年龄：" + students.get(i).getAge() + "请输入您要修改的年龄：");
				String age = sc.nextLine();
				students.get(i).setAge(Integer.parseInt(age));
				System.out.println();
				break;

			case 5:
				// 修改班级
				System.out.print("班级：" + students.get(i).getStu_class() + "请输入您要修改的班级：");
				String stu_class = sc.nextLine();
				students.get(i).setStu_class(stu_class);
				System.out.println();
				break;

			case 6:
				// 修改专业
				MajorModel majorModel = new MajorModel();
				// 专业信息

				int j = 0;
				for (j = 0; j < majorModels.size(); j++) {
					System.out.println(j + 1 + "--------------------");
					System.out.println("专业id" + majorModels.get(j).getmId());
					System.out.println("专业名称" + majorModels.get(j).getMajor());
				}

				int mid = students.get(i).getMarjorId();
				int mi = majorModel.searchById(majorModels, mid);// 专业下标
				if (mi == -1) {
					System.out.println();
				} else {
					System.out.print("专业：" + majorModels.get(mi).getMajor());
				}

				System.out.println("请输入您要修改的专业id：");
				String marjorId = sc.nextLine();

				int m = majorModel.searchById(majorModels, Integer.parseInt(marjorId));
				if (m == -1) {
					System.out.println("专业id不存在！");
				} else {
					students.get(i).setMarjorId(Integer.parseInt(marjorId));// 存入专业id
					majorModels.get(m).setStuId(i + " ");
				}
				break;

			case 7:
				return;

			default:
				break;
			}
			System.out.println("修改成功！");
			message(students, majorModels, i);
		}

	}

	// 删除学生信息
	public void delStudent(ArrayList<StudentModel> students, int i) {
		students.remove(i);
		System.out.println("删除成功！");
	}

}
